<template>
  <section class="loginContainer">
    <Header :title="title">
      <div slot="left" @click="goBack" style="width: 100%;height: 100%;text-align: center;">
        <i class="iconfont icon-arrow-left" style="font-size: 22px"></i>
      </div>
    </Header>
    <div class="loginInner">
      <div class="login_content">
        <form @submit.prevent="submit">
          <div class="on">
            <section class="login_message">
              <input type="tel" maxlength="11" placeholder="手机号" v-model="regphone">
              <button
                :disabled="!rightPhone"
                class="get_verification"
                :class="{right_phone:rightPhone}"
                @click.prevent="getCode"
              >{{computeTime>0 ? `(${computeTime}s)已发送` : '获取验证码'}}</button>
            </section>
            <section class="login_verification">
              <input type="tel" maxlength="6" placeholder="验证码" v-model="regcode">
            </section>
            <section class="login_verification">
              <input type="password" maxlength="20" placeholder="输入新密码" v-model="regpwd">
            </section>
            <section class="login_verification">
              <input type="password" maxlength="20" placeholder="确认新密码" v-model="regrpwd">
            </section>
            <button class="login_submit">确认</button>
          </div>
        </form>
      </div>
    </div>
  </section>
</template>
<script>
import Header from "../../components/Header/Header";
import { reqSendCode, reqMemberResetPWD } from "../../api/index.js";
import { mapState } from "vuex";
export default {
  data() {
    return {
      title: '',
      computeTime: 0,
      regphone: "", //注册手机号
      regcode: "", //注册验证码
      regpwd: "", //注册密码
      regrpwd: "" //注册密码确认
    };
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {});
  },
  mounted(){
    this.title= this.$router.history.current.query.title;
  },
  computed: {
    ...mapState(["curUrl"]),
    rightPhone() {
      // 利用正则对手机号进行匹配，返回布尔值
      return /^1\d{10}$/.test(this.regphone);
    }
  },
  watch:{
    computeTime: function (data) {
      console.log('computeTime', data)
    }
  },
  methods: {
    // 异步获取短信验证码
    async getCode() {
      if(this.computeTime) return;
      if (!this.computeTime) {
        this.computeTime = 30;
        this.intervalId = setInterval(() => {
          this.computeTime--;
          if (this.computeTime <= 0) {
            clearInterval(this.intervalId);
          }
        }, 1000);
      }
      
      // 发送ajax请求（向指定手机号发送验证码短信）
      const result = await reqSendCode({
        mobile: this.regphone,
        event: "resetpwd"
      });
      if (result.code === 1) {
        if (this.computeTime) {
          // this.computeTime = 0;
          // clearInterval(this.intervalId);
          // this.intervalId = undefined;
        }
      }
    },
    // 异步登录
    async submit() {
      let query = {
        type: "mobile",
        mobile: this.regphone,
        captcha: this.regcode,
        password: this.regpwd
      };
      let result = await reqMemberResetPWD(query);
      if (result.code === 1) {
        this.$vux.toast.show(`成功`);
        this.$router.go(-1);
      }else{
        this.$vux.toast.show(`失败:${result.msg}`);
      }
    },
    goBack() {
      if (window.history.length <= 1) {
        this.$router.push({ path: "/" });
        return false;
      } else {
        this.$router.go(-1);
      }
    }
  },
  components: {
    Header
  }
};
</script>
<style lang='less' scoped>
// @import "../../common/stylus/mixins.styl";
@import "../../util/theme.less";

.loginContainer {
  width: 100%;
  height: 100%;
  background: #fff;
  // background: @bg;

  .loginInner {
    padding-top: 10px;
    width: 80%;
    margin: 0 auto;
    overflow: auto;

    .login_header {
      .login_logo {
        font-size: 40px;
        font-weight: bold;
        color: @red;
        text-align: center;
      }

      .login_header_title {
        padding-top: 20px;
        text-align: center;

        > a {
          color: #333;
          font-size: 20px;
          padding-bottom: 4px;

          &:first-child {
            margin-right: 40px;
          }

          &.on {
            color: @red;
            font-weight: 500;
            border-bottom: 2px solid @red;
          }
        }
      }
    }

    .login_content {
      > form {
        > div {
          display: none;

          &.on {
            display: block;
          }

          input {
            width: 100%;
            height: 100%;
            padding-left: 10px;
            box-sizing: border-box;
            border: 1px solid #ddd;
            border-radius: 4px;
            outline: 0;
            font: 400 14px Arial;

            &:focus {
              border: 1px solid @red;
            }
          }

          .login_message {
            position: relative;
            margin-top: 16px;
            height: 48px;
            font-size: 14px;
            background: #fff;

            .get_verification {
              position: absolute;
              top: 50%;
              right: 10px;
              transform: translateY(-50%);
              border: 0;
              color: #ccc;
              font-size: 14px;
              background: transparent;

              &.right_phone {
                color: black;
              }
            }
          }

          .login_verification {
            position: relative;
            margin-top: 16px;
            height: 48px;
            font-size: 14px;
            background: #fff;

            .switch_button {
              font-size: 12px;
              border: 1px solid #ddd;
              border-radius: 8px;
              transition: background-color 0.3s, border-color 0.3s;
              padding: 0 6px;
              width: 30px;
              height: 16px;
              line-height: 16px;
              color: #fff;
              position: absolute;
              top: 50%;
              right: 10px;
              transform: translateY(-50%);

              &.off {
                background: #fff;

                .switch_text {
                  float: right;
                  color: #ddd;
                }
              }

              &.on {
                background: @red;
              }

              > .switch_circle {
                // transform translateX(27px)
                position: absolute;
                top: -1px;
                left: -1px;
                width: 16px;
                height: 16px;
                border: 1px solid #ddd;
                border-radius: 50%;
                background: #fff;
                box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
                transition: transform 0.3s;

                &.right {
                  transform: translateX(14px);
                }
              }
            }
          }

          .login_hint {
            margin-top: 12px;
            color: #999;
            font-size: 14px;
            line-height: 20px;

            > a {
              color: @red;
            }
          }
        }

        .login_submit {
          display: block;
          width: 100%;
          height: 42px;
          margin-top: 30px;
          border-radius: 4px;
          background: @red;
          color: #fff;
          text-align: center;
          font-size: 16px;
          line-height: 42px;
          border: 0;
        }
      }

      .about_us {
        display: block;
        font-size: 12px;
        margin-top: 20px;
        text-align: center;
        color: #999;
      }
    }

    .go_back {
      position: absolute;
      top: 5px;
      left: 5px;
      width: 30px;
      height: 30px;

      > .iconfont {
        font-size: 20px;
        color: #999;
      }
    }
  }
}

.otherLogin {
  // display: flex;
  justify-content: center;
  align-items: center;
  margin: 10px;
  // p{
  //   font-size: 12PX;
  //   display: block;
  //   text-align: center;
  //   margin-bottom: 10px;
  // }
  .title-text {
    margin-bottom: 6px;
  }

  .title-text span {
    display: block; /*设置为块级元素会独占一行形成上下居中的效果*/
    position: relative; /*定位横线（当横线的父元素）*/

    color: #999; /*居中文字的颜色*/
    text-align: center;
    font-size: 12px;
  }

  .title-text span:before,
  .title-text span:after {
    content: "";
    position: absolute; /*定位背景横线的位置*/
    top: 50%;
    background: #8c8c8c; /*背景横线颜色*/
    width: 30%; /*单侧横线的长度*/
    height: 1px;
  }

  .title-text span:before {
    left: 2%; /*调整背景横线的左右距离*/
  }

  .title-text span:after {
    right: 2%;
  }
  .login_types {
    display: flex;
    justify-content: center;
    align-items: center;
    li {
      color: red;
      display: felx;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      i {
        display: block;
        font-size: 36px;
        text-align: center;
        color: #0bbc13;
      }
      b {
        font-size: 12px;
        display: block;
        margin: 6px 0;
        color: #999;
      }
    }
  }
}

.forget_pwd {
  position: absolute;
  right: 6px;
  top: 15px;
  font-size: 12px;
  color: @font-regular;
}
</style>
